package com.dsaa.code2_tree;

import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {

    public static void main(String[] args) {
        TreeNode root = TreeNode.deserialize("1, 2, 3, null, 5, null, 4");

        System.out.println(JSON.toJSONString(root));

        String tree2Nums = TreeNode.serialize(root);

        System.out.println(JSON.toJSONString(tree2Nums));


        List<Integer> integers = rightSideView(root);
        System.out.println(JSON.toJSONString(integers));


    }


    static List<Integer> ans = new ArrayList<>();

    public static List<Integer> rightSideView(TreeNode root) {

        if (root == null) {
            return ans;
        }
        dfs(root, 0);
        return ans;
//        ans.add(root.val);
//        while (root != null) {
//
//            root = ( (root.right == null) ? root.left : root.right);
//
//            if (root != null) {
//                ans.add(root.val);
//            }
//        }
//        return ans;
    }

    private static void dfs(TreeNode root, int depth) {
        if (root == null) {
            return;
        }
        if (ans.size() == depth) {
            ans.add(root.val);
        }
        depth++;
        dfs(root.right, depth);
        dfs(root.left, depth);
    }
}